package youleche.com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

import youleche.com.connection.JdbcHelper;
import youleche.com.idao.ILogInfo;
import youleche.com.model.MLogInfo;

public class DLogInfo implements ILogInfo{

	/**
	 * 记录异常日志
	 * @param con 数据库连接
	 * @param desc 异常描述
	 * @param title 异常标题
	 * @param exception 异常
	 * @param ip ip地址
	 * @throws Exception 抛出异常
	 */
	@Override
	public int addLog(Connection con,  MLogInfo logInfo) throws Exception {
		PreparedStatement pre = null;
		try{
			String sql = "INSERT logInfo(logId,logType,logDesc,logTitle,ip,exception,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,updateTime) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
			pre = con.prepareStatement(sql);
			pre.setObject(1, logInfo.getLogId());
			pre.setObject(2, logInfo.getLogType());
			pre.setObject(3, logInfo.getLogDesc());
			pre.setObject(4, logInfo.getLogTitle());
			pre.setObject(5, logInfo.getIp());
			pre.setObject(6, logInfo.getException());
			pre.setObject(7, logInfo.getA1());
			pre.setObject(8, logInfo.getA2());
			pre.setObject(9, logInfo.getA3());
			pre.setObject(10, logInfo.getA4());
			pre.setObject(11, logInfo.getA5());
			pre.setObject(12, logInfo.getA6());
			pre.setObject(13, logInfo.getA7());
			pre.setObject(14, logInfo.getA8());
			pre.setObject(15, logInfo.getA9());
			pre.setObject(16, logInfo.getA10());
			pre.setObject(17, logInfo.getA11());
			pre.setObject(18, logInfo.getA12());
			pre.setObject(19, logInfo.getA13());
			pre.setObject(20, logInfo.getA14());
			pre.setObject(21, logInfo.getUpdateTime());
	
			return pre.executeUpdate();
		}finally{
			JdbcHelper.free(null, pre);
		}
	}

	/**
	 * 得到日志
	 * @param con 数据库连接
	 * @param logId 日子ID
	 * @return 返回日志
	 * @throws Exception
	 */
	@Override
	public MLogInfo getLogInfoById(Connection con, String logId)
			throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			String sql = "SELECT " +
					"logId,logType,logDesc,logTitle,ip," +
					"exception,A1,A2,A3,A4," +
					"A5,A6,A7,A8,A9," +
					"A10,A11,A12,A13,A14" +
					" FROM logInfo where logId='"+logId+"' limit 1;";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			MLogInfo log = null;
			if(rs != null){
				log = new MLogInfo();
				while(rs.next()){
					log.setLogId(rs.getString(1));
					log.setLogType(rs.getString(2));
					log.setLogDesc(rs.getString(3));
					log.setLogTitle(rs.getString(4));
					log.setIp(rs.getString(5));
					log.setException(rs.getString(6));
					log.setA1(rs.getString(7));
					log.setA2(rs.getString(8));
					log.setA3(rs.getString(9));
					log.setA4(rs.getString(10));
					log.setA5(rs.getString(11));
					log.setA6(rs.getString(12));
					log.setA7(rs.getString(13));
					log.setA8(rs.getString(14));
					log.setA9(rs.getString(15));
					log.setA10(rs.getString(16));
					log.setA11(rs.getString(17));
					log.setA12(rs.getString(18));
					log.setA13(rs.getString(19));
					log.setA14(rs.getString(20));
				}
			}
			
			return log;
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}

	/**
	 * 得到日志信息列表
	 * @param con 数据库连接
	 * @param pageIndex 页面索引
	 * @param pageSize 页大小
	 * @return 列表信息
	 * @throws Exception 异常
	 */
	@Override
	public List<MLogInfo> getLogInfoList(Connection con, String pageIndex,
			String pageSize) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			String sql = "SELECT " +
					"logId,logType,logDesc,logTitle,ip," +
					"exception,A1,A2,A3,A4," +
					"A5,A6,A7,A8,A9," +
					"A10,A11,A12,A13,A14" +
					" FROM logInfo order by updateTime desc limit "+pageIndex+","+pageSize+";";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			MLogInfo log = null;
			List<MLogInfo> list = null;
			if(rs != null){
				list = new LinkedList<MLogInfo>();
				while(rs.next()){
					log = new MLogInfo();
					log.setLogId(rs.getString(1));
					log.setLogType(rs.getString(2));
					log.setLogDesc(rs.getString(3));
					log.setLogTitle(rs.getString(4));
					log.setIp(rs.getString(5));
					log.setException(rs.getString(6));
					log.setA1(rs.getString(7));
					log.setA2(rs.getString(8));
					log.setA3(rs.getString(9));
					log.setA4(rs.getString(10));
					log.setA5(rs.getString(11));
					log.setA6(rs.getString(12));
					log.setA7(rs.getString(13));
					log.setA8(rs.getString(14));
					log.setA9(rs.getString(15));
					log.setA10(rs.getString(16));
					log.setA11(rs.getString(17));
					log.setA12(rs.getString(18));
					log.setA13(rs.getString(19));
					log.setA14(rs.getString(20));
					list.add(log);
				}
			}
			
			return list;
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}

	/**
	 * 得到日志信息总条数
	 * @param con 数据库连接
	 * @return 总条数
	 * @throws Exception
	 */
	@Override
	public String getLogCount(Connection con) throws Exception {
		PreparedStatement ps = null;
		ResultSet rs = null;
		try{
			String sql = "select count(logId) from logInfo";
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs != null){
				while(rs.next()){
					return rs.getString(1);
				}
			}
			
			return "0";
		}finally{
			JdbcHelper.free(rs, ps);
		}
	}
}
